package com.bappi.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;

public class Utils {
	public static final ProgressDialog getProgressDialog(Context context) {
		ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setTitle("Loading");
		progressDialog.setMessage("Please wait...");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setCancelable(true);
		progressDialog.setIndeterminate(true);
		return progressDialog;
	}

	public static void showAlertMessage(Context context, String message) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(final DialogInterface dialog,
							final int id) {

					}
				});
		final AlertDialog alert = builder.create();
		alert.show();
	}

	public static final String[] getUserNames(String text) {
		String[] ret = null;
		if (text != null) {
			Pattern p = Pattern.compile("@([_a-zA-Z0-9^]*):");
			Matcher m = p.matcher(text);
			List<String> ls = new ArrayList<String>();
			while (m.find()) {
				ls.add(m.group(1));
			}
			ret = new String[ls.size()];
			ls.toArray(ret);
		} else {
			ret = new String[0];
		}
		return ret;
	}

	public static String[] getLRLS(String text) {
		String[] ret = null;
		if (text != null) {
			Pattern p = Pattern
					.compile("\\b((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
			Matcher m = p.matcher(text);
			List<String> ls = new ArrayList<String>();
			while (m.find()) {
				ls.add(m.group(1));
			}
			ret = new String[ls.size()];
			ls.toArray(ret);
		} else {
			ret = new String[0];
		}
		return ret;
	}

	public static final boolean putValue(Context context, String key,
			String value) {
		return PreferenceManager.getDefaultSharedPreferences(context).edit()
				.putString(key, value).commit();
	}

	public static final String getValue(Context context, String key) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(key, null);
	}

	public static byte[] getFileData(Context context, String fileName)
			throws Exception {
		byte[] ret = null;
		try {
			InputStream is = context.getAssets().open(fileName);
			byte[] data = new byte[2048];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int read;
			while ((read = is.read(data)) != -1) {
				baos.write(data, 0, read);
			}
			is.close();
			ret = baos.toByteArray();
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static JSONArray getOtherAppsInfo(Context context) throws Exception {
		return new JSONArray(
				new String(getFileData(context, "other_apps.json")));
	}
	public static int getAppLogoId(Context context,String name) {
		int ret = 0;
		try {
			ret = context.getResources().getIdentifier(
					"icon_" + name, "raw",
					context.getClass().getPackage().getName());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	public static float getDPIFactor(Activity activity) {
		DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return metrics.density;
	}
}
